<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的用户信息和视频信息
$user_id = $_POST['user_id'] ?? '';
$video_title = $_POST['video_title'] ?? '';
$video_description = $_POST['video_description'] ?? '';
$video_duration = $_POST['video_duration'] ?? '';
$video_format = $_POST['video_format'] ?? '';
$video_likes = $_POST['video_likes'] ?? '';
$video_link = $_POST['video_link'] ?? '';
$video_remarks = $_POST['video_remarks'] ?? '';
$review_status = $_POST['review_status'] ?? '假';
$reviewer_id = $_POST['reviewer_id'] ?? '';
$rejection_reason = $_POST['rejection_reason'] ?? '';

// 获取上传的视频文件
$video_file = $_FILES['video_file'] ?? null;

// 检查用户 ID 和视频标题是否为空
if (empty($user_id) || empty($video_title)) {
    $result = [
        'status' => 'error',
        'message' => '用户 ID 和视频标题不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 检查视频文件是否上传成功
if (!isset($video_file) || $video_file['error'] !== UPLOAD_ERR_OK) {
    $result = [
        'status' => 'error',
        'message' => '视频文件上传失败'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 设置视频文件上传目录
    $videoUploadDir = 'uploads/videos/';
    if (!file_exists($videoUploadDir)) {
        mkdir($videoUploadDir, 0777, true);
    }

    // 生成唯一的视频文件名
    $videoFileName = uniqid() . '_' . basename($video_file['name']);
    $videoFilePath = $videoUploadDir . $videoFileName;

    // 移动上传的视频文件到目标目录
    if (!move_uploaded_file($video_file['tmp_name'], $videoFilePath)) {
        $result = [
            'status' => 'error',
            'message' => '视频文件移动失败'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    // 插入视频数据到 mi_videos 表
    $insertVideoStmt = $pdo->prepare("INSERT INTO mi_videos (
                上传者用户的id, 视频标题, 视频介绍, 视频时长, 视频格式, 
                获赞数量, 视频链接, 视频备注, 是否已审核, 审核者的用户id, 拒绝原因
            ) VALUES (
                :user_id, :video_title, :video_description, :video_duration, :video_format, 
                :video_likes, :video_link, :video_remarks, :review_status, :reviewer_id, :rejection_reason
            )");

    // 绑定参数
    $insertVideoStmt->bindParam(':user_id', $user_id);
    $insertVideoStmt->bindParam(':video_title', $video_title);
    $insertVideoStmt->bindParam(':video_description', $video_description);
    $insertVideoStmt->bindParam(':video_duration', $video_duration);
    $insertVideoStmt->bindParam(':video_format', $video_format);
    $insertVideoStmt->bindParam(':video_likes', $video_likes);
    $insertVideoStmt->bindParam(':video_link', $videoFilePath);
    $insertVideoStmt->bindParam(':video_remarks', $video_remarks);
    $insertVideoStmt->bindParam(':review_status', $review_status);
    $insertVideoStmt->bindParam(':reviewer_id', $reviewer_id);
    $insertVideoStmt->bindParam(':rejection_reason', $rejection_reason);

    // 执行插入操作
    if ($insertVideoStmt->execute()) {
        // 上传成功
        $result = [
            'status' => 'success',
            'message' => '视频上传成功',
            'video_id' => $pdo->lastInsertId()
        ];
    } else {
        // 删除已上传的视频文件
        unlink($videoFilePath);
        $result = [
            'status' => 'error',
            'message' => '视频上传失败'
        ];
    }

    echo json_encode($result, JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    // 删除已上传的视频文件
    if (isset($videoFilePath) && file_exists($videoFilePath)) {
        unlink($videoFilePath);
    }
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}